﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

public partial class cp_member_profile : OboutInc.oboutAJAXPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        
        int iDaLogin = (Int32)Session["IsLogin"];
        if (iDaLogin == 0)
        {
            Response.Redirect("/Common/Login.aspx");
            return;
        }

        if (!Page.IsPostBack)
        {
            DataBindMemberProfile();
        }

        if ((Session["Reload"] != null && (bool)Session["Reload"]))
        {
            Session["Reload"] = false;
            DataBindMemberProfile();
        }
    }

    public string HashPassword(string pwd)
    {
        string hashedPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(pwd, "sha1");

        return hashedPwd;
    }

    public int ChangePass(string sOldPass, string sNewPass)
    {
        if (Session["UserID"] != null)
        {
            Code.MemberController controller = new Code.MemberController();
            int memID = Int32.Parse(Session["UserID"].ToString());
            if (controller.CheckMemberAccount(memID, HashPassword(sOldPass)) == -1)
                return -1;
            return controller.changePass(memID, HashPassword(sNewPass));
        }
        return -1;
    }


    public string[] LoadInformation()
    {
        if (Session["UserID"] != null)
        {
            Code.MemberController controller = new Code.MemberController();
            return controller.getInfo(Int32.Parse(Session["UserID"].ToString()));
        }
        return null;
    }

    public void SaveInformation(
        string sHoTen, string sDiaChi, string sSDT, string sNgaySinh, string sEmail, string sNgheNghiep)
    {
        Session["Reload"] = true;
        if (Session["UserID"] != null)
        {
            Code.MemberController controller = new Code.MemberController();
            controller.updateInfo(Int32.Parse(Session["UserID"].ToString()),
                sHoTen, sDiaChi, sSDT, sNgaySinh, sEmail, sNgheNghiep
                );
        }
    }

    private void DataBindMemberProfile()
    {
        Code.MemberController controller = new Code.MemberController();
        dvUserProfile.DataSource = controller.GetMemberInfor((Int32)Session["UserID"]);
        dvUserProfile.DataBind();
    }
}